/*******************************************************************************
*  file    : twmainwindow.cpp
*  created : 29.09.2013
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include "twmainwindow.hpp"
#include "ui_twmainwindow.h"

#include <QRegularExpressionValidator>
#include <QDebug>

twMainWindow::twMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::twMainWindow)
{
    ui->setupUi(this);
    validator = new QRegularExpressionValidator(this);
    validator->setRegularExpression(QRegularExpression("\\+380[0-9]{9}"));
    ui->lineEdit->setValidator(validator);
    connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChange(QString))  );
    onTextChange(ui->lineEdit->text());

}

twMainWindow::~twMainWindow()
{
    delete ui;
}

void twMainWindow::onTextChange(const QString & txt)
{
    QString str(txt);
    int pos = 0;
    QValidator::State st  = validator->validate(str,pos);
    qDebug()<<"Validate state "<<st;
    switch(st)
        {
        case QValidator::Invalid :
            ui->lineEdit->setStyleSheet("background-color: rgb(255, 0, 0);");
            break;
        case QValidator::Intermediate:
            ui->lineEdit->setStyleSheet("background-color: rgb(255, 150, 0);");
            break;
        case QValidator::Acceptable:
            ui->lineEdit->setStyleSheet("");
            break;
        }
}
